<div class="layui-card">
	<div class="layui-card-header">
		<h2 class="header-title">设备列表</h2>
		<span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>设备列表</cite></a>
        </span>
	</div>
	<div class="layui-card-body">
		<div class="layui-form toolbar">
			<label>查询方式：</label>
			<div class="layui-input-inline">
				<select name="eqi" id="eql">
					<option value="userId">用户ID搜索</option>
					<option value="devicename">设备编号搜索</option>
					<option value="nickname">用户昵称搜索</option>
				</select>
			</div>
			<input id="queryEI" class="layui-input search-input" type="text" placeholder="请输入" />&emsp;
			<button id="ei-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
		</div>
		<table class="layui-table" id="equi-table" lay-filter="equiList"></table>
	</div>
</div>

<script type="text/html" id="equiBar">
	<a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="edit">解绑</a>
</script>
<script>
	layui.use(['laydate', 'table', 'util', 'config', 'admin', 'index', 'form'], function() {
		var laydate = layui.laydate;
		var index = layui.index;
		var table = layui.table;
		var config = layui.config;
		var util = layui.util;
		var com = layui.common;
		var admin = layui.admin;
		var form = layui.form;

		function showeiList(els) {
			table.render({
				elem: '#equi-table',
				id: 'equi-table',
				data: els,
				page: true,
				cols: [
					[{
							checkbox: true
						}, {
							field: 'addTime',
							title: '用户头像',
							templet: function(b) {
								if(b.headPortrait == null || b.headPortrait == "") {
									var s = '<div><img src="assets/images/mrtx.png" width="50px"  height="50px" /></div>'
									return s
								} else {
									var s = '<div><img src="'
									s += b.headPortrait;
									s += '" width="50px"  height="50px" /></div>'
									return s
								}
							}
						},
						{
							field: 'userId',
							title: '用户id'
						},
						{
							field: 'nickname',
							title: '用户昵称',
							templet: function(b) {
								if(b.nickname != null) {
									if(b.nickname.length == 1) {
										return b.nickname
									} else if(b.nickname.length == 2) {
										var a = b.nickname;
										a = a.substring(0, 1)
										return a + "*"
									} else {
										if(b.nickname != null) {
											var a = b.nickname;
											var x = a.substring(0, 1)
											var c = a.substring(b.nickname.length - 1, b.nickname.length)
											return x + "***" + c
										}
									}
								} else {
									return "未填写"
								}
							}
						}, {
							field: 'deviceName',
							title: '手环编号'
						},
						{
							field: 'name',
							title: '渠道',
							templet: function(d) {
								if(d.name == null) {
									return "松果健康";
								} else {
									return d.name
								}
							}
						},
						{
							field: 'addTime',
							title: '加入时间'
						},
						{
							field: 'addTime',
							title: '上线时间'
						},
						{
							field: 'caozuo',
							title: '操作',
							toolbar: "#equiBar"
						}
					]
				]
			});

		}

		function usergroupListV(quer) {
			$.ajax({
				type: "POST",
				url: groupListPath,
				data: {
					params: JSON.stringify({
						sortOrder: "group_id asc",
						pageSize: 1000,
						pageNumber: 1,
						creatorId: alog.orgId,
						search: quer
					})
				},
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				beforeSend: function(request) {},
				success: function(data) {
					showGroupTable(data.rows);
				},
				error: function(result) {
					console.log(result);
					config.removeToken();
					location.replace('login.html');
				},
			});
		}

		emList('', '');

		function emList(quer, usc) {
			var alog = config.getTempData('loginInfo')
			var a;
			if(alog.orgId == 1) {
				a = ""
			} else {
				a = alog.orgId + ""
			}

			var uid = "";
			var devicename = "";
			var uname = ""

			if(usc == 'nickname') {
				uname = quer
			} else if(usc == 'devicename') {
				devicename = quer
			} else if(usc == 'userId') {
				uid = quer
			}

			$.ajax({
				type: "POST",
				url: equipment,
				data: {
					params: JSON.stringify({
						pageSize: 1000,
						pageNumber: 1,
						orgId: a,
						devicename: devicename,
						userId: uid,
						nickname: uname
					})
				},
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				beforeSend: function(request) {},
				success: function(data) {
					console.log(data)
					showeiList(data)
				},
				error: function(result) {
					console.log(result);
					config.removeToken();
					location.replace('login.html');
				},
			});
		}

		$('#ei-btn-search').click(function() {
			var a = $('#queryEI').val();
			var b = $('#eql').val();
			emList(a, b);
		})

		table.on('tool(equiList)', function(obj) {
			var data = obj.data;
			if(obj.event === 'edit') {
				layer.confirm('确定解绑此设备吗？', function(i) {
					layer.close(i);
					layer.load(2);
					$.ajax({
						type: "POST",
						url: delDevice,
						data: {
							deviceId: data.deviceId
						},
						dataType: "json",
						xhrFields: {
							withCredentials: true
						},
						success: function(data) {
							layer.closeAll('loading');
							if(data.code == 0) {
								layer.msg(data.msg, {
									icon: 1
								});
								usergroupListV();
							} else {
								layer.msg(data.msg, {
									icon: 2
								});
							}
						},
						error: function(result) {
							console.log(result);
						}
					});
				});
			}
		});

	});
</script>